package com.ggh.service.impl;
import	java.awt.Desktop.Action;
import java.io.Serializable;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ggh.dao.PayMapper;
import com.ggh.dao.UserMapper;
import com.ggh.dao.WithDrawMapper;
import com.ggh.entity.Pay;
import com.ggh.entity.User;
import com.ggh.entity.WithDraw;
import com.ggh.service.WithDrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ： jsq
 * @date ：2019/10/13 22:24
 * @description : 提现
 * @path : com.ggh.service.impl.WithDrawServiceImpl
 * @modifiedBy ：
 */
@Service
public class WithDrawServiceImpl extends ServiceImpl<WithDrawMapper, WithDraw> implements WithDrawService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    PayMapper payMapper;

    /**
    * @description :查询相关提现数据
    * @param userid
    * @param type
    * @return: com.ggh.entity.WithDraw
    * @date : 2019/10/16 10:34
    * @author : jj
    * @throws
    */
    @Override
    public WithDraw getByType(Integer userid , Integer type) {
        User user = userMapper.selectById(userid);
        Pay pay = payMapper.selectOne(new QueryWrapper<Pay>().eq("userid", userid).eq("type", type));
        WithDraw withDraw=new WithDraw();
        withDraw.setMoney(user.getUserid());
        withDraw.setUserName(pay.getUserName());
        withDraw.setType(pay.getType());
        if (pay.getType()==1){
            withDraw.setNumber(pay.getZfbCount());
        }else if (pay.getType()==2){
            withDraw.setNumber(pay.getBankCardCode());
        }
        return withDraw;
    }

    /**
     *提现
     * @param withDraw
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean wdDeposit(WithDraw withDraw) {
        User user = userMapper.selectById(withDraw.getUserid());
        if(user!=null){
            user.setMoney(withDraw.getTxmoney());
            userMapper.updateById(user);
            /*提现中*/
            withDraw.setStatus(1);
            withDraw.insert();
            return true;
        }
        return false;
    }

}
